﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Function;

namespace UI.ErrorPage
{
    public partial class ShowMsg : System.Web.UI.Page
    {
        //private string m_Title = "消息提示";

        /// <summary>
        /// 样式名称
        /// </summary>
        public string MsgClassName;

        /// <summary>
        /// 图片
        /// </summary>
        public string MsgPic;

        /// <summary>
        /// 消息标题
        /// </summary>
        public string MsgTitle;

        /// <summary>
        /// 消息内容
        /// </summary>
        public string MsgContent;

        /// <summary>
        /// 消息链接
        /// </summary>
        public string MsgBtn;

        protected void Page_Load(object sender, EventArgs e)
        {
            //base.myPageTitle = ConfigurationManager.AppSettings["SiteName"];//通过基类定义页面标题

            if (!IsPostBack)
            {
                //base.myPageTitle = this.m_Title;

                MessageBox_Page McBx = new MessageBox_Page();
                if (Request.Cookies[MessageEvent.CookieName] != null)
                {
                    string messagecontent = Request.Cookies[MessageEvent.CookieName].Value.ToString();

                    if (!String.IsNullOrEmpty(messagecontent))
                    {
                        McBx = MessageEvent.Deserialize_MessageBox(MessageEvent.Deserialize_MessageBox(messagecontent));
                    }
                }
                this.MsgClassName = this.GetMsgClassName(McBx.Type);
                this.MsgPic = this.GetMsgPic(McBx.Type);
                this.MsgTitle = McBx.Title;
                this.MsgContent = this.GetMsgContent(McBx.ContentItems);
                this.MsgBtn = this.GetMsgBtn(McBx.UrlItems);
            }
        }

        /// <summary>
        /// 获得样式名称
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private string GetMsgClassName(MsgType type)
        {
            string classname;
            switch (type)
            {
                case MsgType.Alarm:
                    classname = "MsgTitleColorA";
                    break;
                case MsgType.Error:
                    classname = "MsgTitleColorE";
                    break;
                case MsgType.Success:
                    classname = "MsgTitleColorS";
                    break;
                default://MsgType.Normal
                    classname = "MsgTitleColorS";
                    break;
            }
            return classname;
        }

        /// <summary>
        /// 获得样式图片
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private string GetMsgPic(MsgType type)
        {
            string pic;
            switch (type)
            {
                case MsgType.Alarm:
                    pic = "AlertPic";
                    break;
                case MsgType.Error:
                    pic = "ErrorPic";
                    break;
                case MsgType.Success:
                    pic = "SucceedPic";
                    break;
                default://MsgType.Normal
                    pic = "SucceedPic";
                    break;
            }
            return pic;
        }

        /// <summary>
        /// 获得消息内容
        /// </summary>
        /// <param name="MCItems"></param>
        /// <returns></returns>
        private string GetMsgContent(IList<MessageContent> MCItems)
        {
            string msgcontent = "";
            foreach (MessageContent MCItem in MCItems)
            {
                msgcontent += string.Format("<li>{0}</li>", MCItem.Content);
            }
            return msgcontent;
        }

        /// <summary>
        /// 获得消息链接
        /// </summary>
        /// <param name="MUItems"></param>
        /// <returns></returns>
        private string GetMsgBtn(IList<MessageUrl> MUItems)
        {
            string msgbtn = "";
            foreach (MessageUrl MUItem in MUItems)
            {
                string even;
                switch (MUItem.Type)
                {
                    case UrlType.Javascript:
                        even = string.Format("JavaScript:{0}", MUItem.Url);
                        break;
                    default://UrlType.Href
                        even = string.Format("JavaScript:window.location.href='{0}';", MUItem.Url);
                        break;
                }
                msgbtn += string.Format("<input type=\"button\" class=\"MsgBtn\" onclick=\"{0}\" title=\"{1}\" value=\"{2}\" />", even, MUItem.Hint, MUItem.Text);
            }
            return msgbtn;
        }
    }
}